package com.cxs.service.impl;

import com.cxs.dto.ReportDTO;
import com.cxs.enums.HospitalErrorResultEnum;
import com.cxs.exception.HospitalException;
import com.cxs.mapper.ReportMapper;
import com.cxs.pojo.Report;
import com.cxs.pojo.ReportExample;
import com.cxs.service.ReportService;
import com.cxs.vo.ReportIndexVO;
import com.cxs.vo.Result;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.weaver.ast.Var;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/*
 * @Author:cxs
 * @Date:2021/10/19  16:34
 * @Version 1.0
 * @Motto:放下杂念,只为迎接明天更好的自己
 * */
@Slf4j
@Service
public class ReportServiceImpl implements ReportService {

    private static final Integer DELETED = 0;
    @Autowired
    private ReportMapper reportMapper;

    @Override
    public Long reportCount() {
        ReportExample example = new ReportExample();
        ReportExample.Criteria criteria = example.createCriteria();
        criteria.andStateNotEqualTo(DELETED);
        return reportMapper.countByExample(example);
    }

    @Override
    public Long todayCount() {
        return reportMapper.countByToday();
    }

    @Override
    public List<ReportIndexVO> getList(String cc, String name) {
        List<ReportIndexVO> list = new ArrayList<>();

        Integer code = null;
        try {
            code = Integer.parseInt(cc);
        } catch (NumberFormatException e) {
            log.error("【挂号模块】: {} 转换异常", cc);
        }

        List<Report> reportList = reportMapper.selectListByCondition(code, name);

        reportList.forEach(r -> {
            ReportIndexVO vo = new ReportIndexVO();
            BeanUtils.copyProperties(r, vo);
            list.add(vo);
        });
        return list;
    }

    @Override
    public Integer checkPhoneAndCid(String phone, String carid) {

        List<Report> reportList = reportMapper.selectByPhoneOrCarid(phone, carid);

        if (reportList == null) {
            log.error("【门诊模块】：查询结果异常,list = {}", reportList);
            throw new HospitalException(500, "【门诊模块】：查询结果异常");
        }

        if (reportList.size() == 0) {
            return 3;
        }

        Report report = reportList.get(0);

        if (report.getPhone() != null && phone.equals(report.getPhone())) {
            return 1;
        }

        if (report.getCarid() != null && carid.equals(report.getCarid())) {
            return 2;
        }

        return -1;
    }

    @Override
    public void addReport(ReportDTO reportDTO) {
        Report report = new Report();

        BeanUtils.copyProperties(reportDTO, report);

        // 单独处理
        report.setDepartment(reportDTO.getDepartmentId());
        report.setDoctor(reportDTO.getDoctorId());
        report.setReportType(reportDTO.getRegisterTypeId());

        int result = reportMapper.insert(report);

        if (result != 1) {
            log.error("【添加门诊】:门诊添加失败 report = {}", report);
            throw new HospitalException(HospitalErrorResultEnum.REPORT_SAVE_ERROR);
        }
    }

    @Override
    public void del(Integer id) {
        Report report = new Report();
        report.setReportId(id);
        report.setState(0);

        int result = reportMapper.updateByPrimaryKeySelective(report);
        if (result != 1) {
            log.error("【删除门诊】:门诊删除失败 reportId = {}", id);
            throw new HospitalException(HospitalErrorResultEnum.REPORT_DELETE_ERROR);
        }
    }

    @Override
    public PageInfo<Report> getPageInfo(String name, Integer pageNo, Integer limit) {

        PageHelper.startPage(pageNo, limit);
        List<Report> reportList = reportMapper.selectListByKeyword(name);
        return new PageInfo<>(reportList);
    }

    @Override
    public Report getBing(Integer reportId) {
        return reportMapper.selectByPrimaryKey(reportId);
    }

    @Override
    public void update(Report report) {
        int result = reportMapper.updateByPrimaryKeySelective(report);
        if (result != 1) {
            log.error("【删除门诊】:门诊删除失败 report = {}", report);
            throw new HospitalException(HospitalErrorResultEnum.REPORT_UPDATE_ERROR);
        }
    }
}
